package com.atguigu.tingshu.order.strategy.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.IdUtil;
import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.order.helper.SignHelper;
import com.atguigu.tingshu.order.strategy.TradeDataStrategy;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Slf4j
@Component("1002")
public class TrackTradeDataStrategy implements TradeDataStrategy {
    @Autowired
    private AlbumFeignClient albumFeignClient;
    @Override
    public void handlerTradeData(TradeVo tradeVo,OrderInfoVo orderInfoVo) {

        //1.1 初始化订单结算页三个价格：原价、减免价、订单价
        BigDecimal originalAmount = new BigDecimal("0.00");
        BigDecimal derateAmount = new BigDecimal("0.00");
        BigDecimal orderAmount = new BigDecimal("0.00");

        //1.2 初始化订单结算页两个集合：商品明细、商品优惠集合
        List<OrderDetailVo> orderDetailVoList = new ArrayList<>();
        List<OrderDerateVo> orderDerateVoList = new ArrayList<>();
        //4 处理购买商品类别：声音
        //4.1 远程调用专辑服务返回待结算声音列表
        List<TrackInfo> waitBuyTrackList = albumFeignClient.getWaitBuyTrackList(tradeVo.getItemId(), tradeVo.getTrackCount()).getData();
        Assert.notNull(waitBuyTrackList, "不存在待结算声音{}", tradeVo.getItemId());

        //4.2 远程调用专辑服务查询专辑信息，得到声音单价
        Long albumId = waitBuyTrackList.get(0).getAlbumId();
        AlbumInfo albumInfo = albumFeignClient.getAlbumInfo(albumId).getData();
        Assert.notNull(albumInfo, "专辑{}不存在", albumId);
        BigDecimal price = albumInfo.getPrice();

        //4.3 计算相关价格
        originalAmount = price.multiply(BigDecimal.valueOf(tradeVo.getTrackCount()));
        orderAmount = originalAmount;

        //4.4 封装订单明细列表
        orderDetailVoList =waitBuyTrackList
                .stream()
                .map(trackInfo -> {
                    OrderDetailVo orderDetailVo = new OrderDetailVo();
                    orderDetailVo.setItemId(trackInfo.getId());
                    orderDetailVo.setItemName("声音：" + trackInfo.getTrackTitle());
                    orderDetailVo.setItemUrl(trackInfo.getCoverUrl());
                    orderDetailVo.setItemPrice(price);
                    return orderDetailVo;
                }).collect(Collectors.toList());
        orderInfoVo.setOriginalAmount(originalAmount);
        orderInfoVo.setDerateAmount(derateAmount);
        orderInfoVo.setOrderAmount(orderAmount);
        orderInfoVo.setOrderDetailVoList(orderDetailVoList);
        orderInfoVo.setOrderDerateVoList(orderDerateVoList);
    }
}
